package com.kelvin.oocl.crm.controller;

import java.io.PrintStream;
import java.util.Scanner;

import com.kelvin.oocl.crm.ApplicationInitializer;
import com.kelvin.oocl.crm.entity.Command;
import com.kelvin.oocl.crm.entity.Customer;
import com.kelvin.oocl.crm.exception.ParameterBadFormatException;
import com.kelvin.oocl.crm.filter.SystemFilter;
import com.kelvin.oocl.crm.service.CommandParser;
import com.kelvin.oocl.crm.util.FormatPrinter;
import com.kelvin.oocl.crm.util.MyLinkList;

public class CommandDispatcher {
	private PrintStream out = System.out;
	private Scanner scan = new Scanner(System.in);
	private CommandParser parser;
	
	//启动方法
	public void startUp(){
		parser = ApplicationInitializer.getApplicationContext().findBean("customerService",CommandParser.class);
		FormatPrinter.showTipHelp(out);
		while(true){
			out.print("pls input->");
			String input = scan.nextLine();
			if("".equals(input.trim())){
				continue;
			}
			Command command = SystemFilter.getInstance().commandFilter(input);
			if(command.getParameters()==null){
				FormatPrinter.showCommondNotFound(command.getCommond(), out);
			}else {
				try {
					dispatcher(command);
				} catch (ParameterBadFormatException e) {
					FormatPrinter.printBadParameterMsg(e, out);
				}
			}
		}
	}
	
	public void dispatcher(Command command) throws ParameterBadFormatException{
		switch (command.getCommond()) {
			case "L":
				MyLinkList<Customer> customers = parser.parseFind(command);
				FormatPrinter.printCustomers(customers, out);
				break;
				
			case "A":
				boolean addResult = parser.parseAdd(command);
				FormatPrinter.printOperationResult(addResult, out);
				break;
				
			case "D":
				boolean deleteResult = parser.parseDelete(command);
				FormatPrinter.printOperationResult(deleteResult, out);
				break;
				
			case "U":
				boolean updateResult = parser.parseUpdate(command);
				FormatPrinter.printOperationResult(updateResult, out);
				break;
				
			case "O":
				MyLinkList<Customer> sortResult = parser.parseSort(command);
				FormatPrinter.printCustomers(sortResult, out);
				break;
				
			case "Q":
				scan.close();
				out.close();
				System.exit(0);
				break;
				
			case "H":
				this.showHelp(command);
				break;
				
			default:
				break;
		}
	}
	
	public void showHelp(Command command) throws ParameterBadFormatException{
		if(command.getParameters()[0].equals("noparameter")){
			FormatPrinter.showHelp(out);
		}else {
			try {
				String[] parameters = command.getParameters();
				switch (parameters[0]) {
					case "A":
						FormatPrinter.showA(out);
						break;
					case "L":
						FormatPrinter.showL(out);
						break;
					case "D":
						FormatPrinter.showD(out);
						break;
					case "U":
						FormatPrinter.showU(out);
						break;
					case "O":
						FormatPrinter.showO(out);
						break;
					case "Q":
						FormatPrinter.showQ(out);
						break;
					default:
						out.println("  No such commond of "+parameters[0]);
						break;
				}
			} catch (ArrayIndexOutOfBoundsException e) {
				throw new ParameterBadFormatException(FormatPrinter.badFormatMsg);
			}
		}
	}
}
